package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.dao.SettingsDao;
import com.beiding.pojo.order.Order;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WithholdDeposit extends TimeoutListener {

    protected WithholdDeposit(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "withholdDeposit");
    }
    private SettingsDao settings;

    @Autowired
    public void setSettings(SettingsDao settings) {
        this.settings = settings;
    }


    private MessageSender sender;

    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }
    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);

        if (judge(order, Order.WITHHOLD_DEPOSIT)) {
            withholdDeposit(order);
            orderDao.updateField(id, "state", Order.EVALUATE);
            timeShaft.delay("evaluate", settings.getTimeout("evaluate"), order.getId());

            order.setState(Order.EVALUATE);

            sender.sendToTenant(order.getHolderId(),update(order));

            sender.sendToLandlord(order.getLandlord(),update(order).msg("房东提出扣留扣留一部分押金,而您没有及时处理,现已按照房东提出的方案处理"));


        }
    }

    private void withholdDeposit(Order order) {
        System.out.println("按照房东的方案进行退还押金操作");
    }


}

